//---------------------------------------------------------------------------

#ifndef t_mainformH
#define t_mainformH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <System.Actions.hpp>
#include <Vcl.ActnList.hpp>
#include <Vcl.Menus.hpp>
#include <Vcl.ImgList.hpp>
#include <Vcl.ComCtrls.hpp>
#include <Vcl.ToolWin.hpp>
#include <Vcl.Dialogs.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Vcl.Graphics.hpp>
//---------------------------------------------------------------------------
const int HM_TRANSFERPROGRESS         = WM_USER + 100;
const int HM_TRANSFERCOMPLETE         = WM_USER + 101;
const int HM_TRANSFERCANCELED         = WM_USER + 102;

class HStage;
class HTransferThread;
class HDesignControl;
class HCommand;
class HCmdAddGroup;
class HCmdRemoveGroup;
class HCmdMoveGroup;
class HCmdLoadGroup;
class HCmdEditBatchProperty;
class HProgressBar;

class TMainForm : public TForm
{
__published:	// IDE-managed Components
	TActionList *ActionList1;
	TAction *FileExit;
	TMainMenu *MainMenu1;
	TMenuItem *File1;
	TMenuItem *N1;
	TMenuItem *FileExit1;
	TAction *HelpAbout;
	TMenuItem *Help1;
	TMenuItem *About1;
   TImageList *ImageList1;
   TToolBar *ToolBarMain;
   TToolButton *ButtonNew;
   TToolButton *ButtonOpen;
   TToolButton *ButtonSave;
   TToolButton *ButtonHome;
   TToolButton *ButtonTransfer;
   TToolButton *ButtonList;
   TToolButton *ButtonUndo;
   TToolButton *ButtonRedo;
   TToolButton *ButtonCut;
   TToolButton *ButtonCopy;
   TToolButton *ButtonPaste;
   TToolButton *ButtonSelect;
   TToolButton *ButtonLink;
   TToolButton *ButtonDelete;
   TToolButton *ButtonJob;
   TToolButton *ButtonConnector;
   TToolButton *ButtonExternJob;
   TToolButton *ButtonEventFire;
   TToolButton *ButtonEventWait;
   TToolButton *ButtonGroup;
   TAction *FileNew;
   TMenuItem *New1;
   TPageControl *PageControl1;
   TOpenDialog *OpenDialog1;
   TSaveDialog *SaveDialog1;
   TPopupMenu *PopupMenu1;
   TAction *FileOpen;
   TMenuItem *Open1;
   TAction *FileSave;
   TAction *FileSaveAs;
   TMenuItem *Save1;
   TMenuItem *SaveAs1;
   TPopupMenu *PopupMenu2;
   TMenuItem *GoToThis2;
   TMenuItem *Home2;
   TMenuItem *NewGroup1;
   TMenuItem *MoveGroupLeft1;
   TMenuItem *MoveGroupRight1;
   TMenuItem *N3;
   TMenuItem *Cut2;
   TMenuItem *Copy2;
   TMenuItem *Paste2;
   TMenuItem *Delete2;
   TMenuItem *SelectAll2;
   TMenuItem *EditProperty2;
   TAction *EditUndo;
   TAction *EditRedo;
   TAction *EditSelectAll;
   TAction *EditDelete;
   TAction *EditEditProperty;
   TAction *EditCut;
   TAction *EditCopy;
   TAction *EditPaste;
   TAction *EditSelectHome;
   TAction *EditSelectEnd;
   TAction *FileChangeSkin;
   TAction *FileList;
   TAction *FileChangeLineMode;
   TAction *GroupNew;
   TAction *GroupDelete;
   TAction *GroupMoveLeft;
   TAction *GroupMoveRight;
   TAction *GroupLoad;
   TAction *GroupInfo;
   TAction *GroupMoveTo;
   TAction *GroupGoto;
   TAction *GroupGotoThis;
   TAction *GroupHome;
   TAction *ServerTransfer;
   TAction *FileDownloadFile;
   TMenuItem *ChangeSkin1;
   TMenuItem *ChangeLineMode1;
   TMenuItem *JobList1;
   TMenuItem *DownloadFile1;
   TMenuItem *Edit1;
   TMenuItem *Undo1;
   TMenuItem *Redo1;
   TMenuItem *N2;
   TMenuItem *Cut1;
   TMenuItem *Copy1;
   TMenuItem *Paste1;
   TMenuItem *Delete1;
   TMenuItem *SelectAll1;
   TMenuItem *EditProperty1;
   TMenuItem *Group1;
   TMenuItem *NewGroup2;
   TMenuItem *MoveGroupLeft2;
   TMenuItem *MoveGroupRight2;
   TMenuItem *MoveGroupTo1;
   TMenuItem *LoadGroup1;
   TMenuItem *GroupProperty1;
   TMenuItem *N4;
   TMenuItem *Home1;
   TMenuItem *GoToThis1;
   TMenuItem *GoTo1;
   TMenuItem *Server1;
   TMenuItem *ransfer1;
   TToolButton *ToolButton1;
   TToolButton *ToolButton2;
   TToolButton *ButtonUp;
   TPanel *Panel1;
   TPanel *Panel2;
   TImage *Image2;
   TToolButton *ToolButton3;
   TAction *GroupUp;
   TAction *FileExport;
   TMenuItem *N5;
   TMenuItem *ExportToExcel1;
   TAction *FileImport;
   TMenuItem *ImportFromExcel1;
   TMenuItem *DeleteGroup1;
   TToolButton *ButtonBatch;
   TToolButton *ButtonTime;
	void __fastcall FileExitExecute(TObject *Sender);
	void __fastcall HelpAboutExecute(TObject *Sender);
   void __fastcall FormShow(TObject *Sender);
   void __fastcall FileNewExecute(TObject *Sender);
   void __fastcall FileOpenExecute(TObject *Sender);
   void __fastcall FileSaveExecute(TObject *Sender);
   void __fastcall FileSaveAsExecute(TObject *Sender);
   void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
   void __fastcall FormDeactivate(TObject *Sender);
   void __fastcall PageControl1Change(TObject *Sender);
   void __fastcall ActionList1Update(TBasicAction *Action, bool &Handled);
   void __fastcall ToolButtonClick(TObject *Sender);
   void __fastcall EditUndoExecute(TObject *Sender);
   void __fastcall EditRedoExecute(TObject *Sender);
   void __fastcall EditSelectAllExecute(TObject *Sender);
   void __fastcall EditDeleteExecute(TObject *Sender);
   void __fastcall EditEditPropertyExecute(TObject *Sender);
   void __fastcall EditCutExecute(TObject *Sender);
   void __fastcall EditCopyExecute(TObject *Sender);
   void __fastcall EditPasteExecute(TObject *Sender);
   void __fastcall EditSelectHomeExecute(TObject *Sender);
   void __fastcall EditSelectEndExecute(TObject *Sender);
   void __fastcall ServerTransferExecute(TObject *Sender);
   void __fastcall GroupNewExecute(TObject *Sender);
   void __fastcall GroupDeleteExecute(TObject *Sender);
   void __fastcall GroupMoveLeftExecute(TObject *Sender);
   void __fastcall GroupMoveRightExecute(TObject *Sender);
   void __fastcall GroupLoadExecute(TObject *Sender);
   void __fastcall GroupInfoExecute(TObject *Sender);
   void __fastcall GroupMoveToExecute(TObject *Sender);
   void __fastcall GroupGotoExecute(TObject *Sender);
   void __fastcall GroupGotoThisExecute(TObject *Sender);
   void __fastcall GroupHomeExecute(TObject *Sender);
   void __fastcall FileChangeSkinExecute(TObject *Sender);
   void __fastcall FileListExecute(TObject *Sender);
   void __fastcall FileChangeLineModeExecute(TObject *Sender);
   void __fastcall FileDownloadFileExecute(TObject *Sender);
   void __fastcall GroupUpExecute(TObject *Sender);
   void __fastcall FileExportExecute(TObject *Sender);
   void __fastcall FileImportExecute(TObject *Sender);

public:		// User declarations
        friend HCmdAddGroup;
        friend HCmdRemoveGroup;
        friend HCmdMoveGroup;
        friend HCmdLoadGroup;
        friend HCmdEditBatchProperty;
        typedef enum { caSelect, caLink, caUnlink, caAddStage } HCurrentAction;
        HCurrentAction CurrentAction;
        bool ChangeFlag;
        UTF8String StageType;
        int MaxDesignerWidth;
        int MaxDesignerHeight;
        HProgressBar *ProgressBar;
        struct HInfo
        {
           UTF8String Author;
           UTF8String JobTrigger;
           UTF8String BatchTrigger;
           bool AutoSkip;
           UTF8String AutoRerun;
           UTF8String Description;
           UTF8String IncludeFiles;
           bool CheckBeforeTransfer;
           UTF8String FadingPeriod;
           UTF8String PartitionDither;
           UTF8String SectionList;
           UTF8String GroupName;
           UTF8String Banner;
           int BannerPosition;
           UTF8String BannerFontName;
           int BannerFontSize;
           int BannerFontStyle;
           bool AcrossDayEnd;
           UTF8String TrafficControl;
           HInfo()
              : AutoSkip(false), CheckBeforeTransfer(true),
                BannerPosition(1), BannerFontName("Arial"),
                BannerFontSize(8), BannerFontStyle(0), AcrossDayEnd(false)
           {}
        };
        __fastcall TMainForm(TComponent* Owner);
        void __fastcall RefreshRecentJobList(const list<UTF8String> &List);
        void __fastcall PushUndoStack(HCommand *Command);
        void __fastcall GetInfo(HInfo &Info);
        bool __fastcall SetInfo(const HInfo &Info, UTF8String &ErrorString);
        bool __fastcall ControlExist(const UTF8String &Name);
        int __fastcall GetPageIndex();
        void __fastcall SetPageIndex(int PageIndex);
        void __fastcall RemovePage(int PageIndex);
        UTF8String __fastcall GetGroupName(int PageIndex);
        void __fastcall MoveGroup(int IndexFrom, int IndexTo);
        void __fastcall DoEditBatchProperty();
        HDesignControl * __fastcall CurrentDesignControl(void);
        void __fastcall RefreshCaption();
        void __fastcall SaveToJobList(map<UTF8String, HJobListInfo> &JobMap);
        HStage * __fastcall FindControlByName(const UTF8String &Name);
        void __fastcall GotoGroupAndFocusToJob(const UTF8String &Name);
        int __fastcall NewGroup(const UTF8String &Name = "",
           HXmlTree *Node = NULL, bool XmlPropertyFlag = false, bool MakePageActive = true);
private:	// User declarations
        bool __fastcall IsChanged();
        bool __fastcall Save(const UTF8String &FileName);
        bool __fastcall ExcelExport(const UTF8String &FileName);
        bool __fastcall Load(const UTF8String &FileName);
        bool __fastcall ExcelImport(const UTF8String &FileName);
        bool __fastcall SaveAs(const UTF8String &FileName);
        void __fastcall SaveInfoToXmlTree(HXmlTree *XmlTree);
        void __fastcall SaveToXmlTree(HXmlTree *XmlTree);
        void __fastcall LoadInfoFromXmlTree(HXmlTree *XmlTree, bool XmlPropertyFlag);
        void __fastcall LoadFromXmlTree(HXmlTree *XmlTree, bool XmlPropertyFlag);
        void __fastcall RemoveAll(void);
        void __fastcall UnselectControl(void);
        bool __fastcall GroupExist(const UTF8String &Name);
        void __fastcall InitUndoStack(void);
        void __fastcall ClearUndoStack(void);
        UTF8String __fastcall GetUndoCaption(void);
        UTF8String __fastcall GetRedoCaption(void);
        void __fastcall Undo(void);
        void __fastcall Redo(void);
        void __fastcall SetCell(Variant &Sheet, int Row, int Column,
            const char *Value);
        void __fastcall ExcelExport_aux(vector<HXmlTree*> &GroupVec,
           Variant &WorkBook, Variant &Sheet, int &ProcessedJobQty, int &TotalJobQty);

        struct HUndoInfo
        {
           int GroupIndex;
           int HScrollPosition;
           int VScrollPosition;
           HXmlTree *XmlTree;
           UTF8String Type;
           UTF8String Object;
        };

        struct HGroupInfo
        {
           TTabSheet *TabSheet;
           HDesignControl *DesignControl;
        };

        UTF8String FileName;

        struct group_info
        {
           string Name;
           int JobQty;
           string Agent;
        };

        void __fastcall OpenMenuItemClick(TObject *Sender);
        void __fastcall HMTransferProgress(TMessage &Message);
        void __fastcall HMTransferComplete(TMessage &Message);
        void __fastcall HMTransferCanceled(TMessage &Message);
        bool __fastcall CheckCircuit(void);

        HTransferThread *TransferThread;
        HInfo FInfo;
        vector<HCommand*> FUndoStack;
        int FUndoIndex;
        int FSavedUndoIndex;
        vector<HGroupInfo> Groups;

protected :
        BEGIN_MESSAGE_MAP
           VCL_MESSAGE_HANDLER(HM_TRANSFERPROGRESS, TMessage,
              HMTransferProgress);
           VCL_MESSAGE_HANDLER(HM_TRANSFERCOMPLETE, TMessage,
              HMTransferComplete);
           VCL_MESSAGE_HANDLER(HM_TRANSFERCANCELED, TMessage,
              HMTransferCanceled);
        END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif
